const pageQuery = require('../utils/pageQuery')
const AsyncMysqljs = require('../mysql/index')
const Query = AsyncMysqljs().query

module.exports = async (ctx, next) => {
    const {page, pageSize, value} = ctx.request.query
    
    
    let init_page= page || 1; //默认页码为1
    let num = pageSize || 10; //每页要显示的数据量
    let start = 0; //设置起始数据为第一条
    if(init_page > 1) start = (init_page - 1) * num; //页码减去1，乘以条数就得到分页的起始数了
    let filmListSql = ''
    let list = []
    let pageCount = 0
    const codeParams = 'id, name, phone, address, create_date'
    console.log(num);
    try {
        if(value) {
            filmListSql = `SELECT ${codeParams} FROM film WHERE type=? AND is_show=? ORDER BY id DESC limit ${start},${num}`
            list = await Query(filmListSql,[type, 1])
            pageCount = await Query('SELECT COUNT(*) FROM film WHERE type=? AND is_show=?', [value, 1])
        } else {
            filmListSql = `SELECT ${codeParams} FROM user_info ORDER BY id DESC limit ${start},${num} `
            pageCount = await Query('SELECT COUNT(*) FROM film')
            list = await Query(filmListSql, [1])
        }
    } catch (error) {
        console.log(error);
        
    }
    console.log(list);
    
    ctx.body = {
        code: 1,
        data: {
            list: list,
            page: init_page,
            pageSize: num,
            pageCount: Math.ceil(pageCount[0]['COUNT(*)'] / num),
            total: pageCount[0]['COUNT(*)']
            // li: '0000'
        },
        msg: 'ok'
    }
}